package com.cuit.service.Impl;

import com.cuit.pojo.Doctor;
import com.cuit.dao.MangerDao;
import com.cuit.dao.impl.MangerDaoImpl;
import com.cuit.pojo.Manger;
import com.cuit.pojo.Page;
import com.cuit.service.MangerService;

import java.util.List;

public class MangerServiceImpl implements MangerService {

    private MangerDao mangerDao = new MangerDaoImpl();

    public Manger login(Manger manger) {
        return mangerDao.queryUserByUsernameAndPassword(manger.getUsername(), manger.getPsw());
    }

    @Override
    public Page<Doctor> queryDoctorByPage(int pageNo, int pageSize) {
        Page<Doctor> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = mangerDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        int pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Doctor> doctors = mangerDao.queryDoctorByPage(begin, pageSize);
        page.setItems(doctors);
        return page;
    }

    @Override
    public void add() {
        String password = "123456";
        Doctor doctor = new Doctor();
        doctor.setPassword(password);
        mangerDao.addDoctor(doctor);
    }

    @Override
    public void delete(int id) {
        mangerDao.deleteDoctorById(id);
    }


}
